#pragma GCC optimize(2)
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

namespace my {
    const int maxn=600000, maxq=300000, maxk=19, inf=0x7ffffff;

    int n, m=1, ind[2*maxn+1], to[2*maxn+1], link[2*maxn+1], val[2*maxn+1], up[2*maxn+1][maxk+1], dep[2*maxn+1], nval[maxn+1], ups[maxn+1], sbook[2*maxn+1], que[maxn+1][2], st[2*maxn+1][maxk+1], fir[maxn+1], log2[maxn+1];

    void read(int &a) {
        a=0;
        char c;
        for (c=getchar(); c<'0' || c>'9'; c=getchar());
        for (; c>='0' && c<='9'; c=getchar()) a=a*10+c-'0';
    }

    void initLog(int n) {
        for (int i=2; i<=n; i++) log2[i] = log2[i>>1]+1;
    }

    void addSide(int u, int v, int w=2) {
        m++;
        to[m] = v;
        val[m] = w;
        link[m] = ind[u];
        ind[u] = m;
    }

    void getBfs(int que[][2]) {
        int h=0, t=1;
        for (int i=1; i<=n; i++) que[i][0]=que[i][1] = 0;
        que[1][0] = 1;
        que[1][1] = 0;
        for (; h<t;) {
            int o=que[++h][0], fa=que[h][1];
            for (int j=ind[o]; j; j=link[j]) {
                if (!sbook[j] && to[j]!=fa) {
                    que[++t][0] = to[j];
                    que[t][1] = o;
                }
            }
        }
    }

    void build() {
        getBfs(que);
        static int size[maxn+1], eu[2*maxn+1];
        int t, i, o, fa;
        for (t=1; t<=n; t++) {
            o=que[t][0], fa=que[t][1];
            dep[o] = dep[fa]+1;
            for (i=1; i<=maxk; i++) {
                up[o][i] = up[up[o][i-1]][i-1];
            }

            for (i=ind[o]; i; i=link[i]) {
                if (to[i]!=fa) {
                    ups[to[i]] = i;
                    up[to[i]][0] = o;
                }
            }
        }

        for (int t=n; t>0; t--) {
            o=que[t][0], fa=que[t][1];
            size[o] = 1;
            for (i=ind[o]; i; i=link[i]) {
                if (to[i]!=fa) {
                    size[o] += size[to[i]];
                }
            }
        }

        fir[1] = 1;
        for (t=1; t<=n; t++) {
            o=que[t][0], fa=que[t][1];
            eu[fir[o]] = o;
            int sum=fir[o];
            for (i=ind[o]; i; i=link[i]) {
                if (to[i]!=fa) {
                    fir[to[i]] = sum+1;
                    sum += 2*size[to[i]];
                    eu[sum] = o;
                }
            }
        }
        for (t=1; t<=2*n-1; t++) st[t][0] = dep[eu[t]];

        for (i=1; i<=log2[2*n-1]; i++) {
            for (t=1; t+(1<<i)-1<=2*n-1; t++) {
                o=t+(1<<(i-1));
                st[t][i] = min(st[t][i-1], st[o][i-1]);
            }
        }
    }

    int getStMin(int l, int r) {
        int len=log2[r-l+1];
        return min(st[l][len], st[r-(1<<len)+1][len]);
    }

    int dis(int u, int v) {
        int d = fir[u]<fir[v] ? getStMin(fir[u], fir[v]) : getStMin(fir[v], fir[u]);
        return (dep[u]+dep[v]-2*d)*2;
    }

    bool tryDoMid(int x, int xl, int len) {
        if (xl==len/2) {
            nval[x] = min(len/2, nval[x]);
            return true;
        } else if (xl==len/2-1) {
            if (!sbook[ups[x]]) {
                sbook[ups[x]]=++n;
                sbook[ups[x]^1]=n;
                addSide(x, n, 1);
                addSide(n, x, 1);
                addSide(up[x][0], n, 1);
                addSide(n, up[x][0], 1);
            }
            nval[sbook[ups[x]]] = min(nval[sbook[ups[x]]], len/2);
            return true;
        }
        return false;
    }

    void getMid(int u, int v, int len) {
        if (dep[u]<dep[v]) {
            int temp;
            temp=u, u=v, v=temp;
        }
        int x=u, y=v, xl=0, yl=0;

        for (int i=maxk; i>=0; i--) {
            if (dep[up[x][i]]>=dep[v] && xl+(1<<i)*2<=len/2) {
                xl += (1<<i)*2;
                x = up[x][i];
            }
        }

        for (int i=maxk; i>=0; i--) {
            if (up[x][i]!=up[y][i] && xl+(1<<i)*2<=len/2 && yl+(1<<i)*2<=len/2) {
                xl+=(1<<i)*2, yl+=(1<<i)*2;
                x=up[x][i], y=up[y][i];
            }
        }

        if (tryDoMid(x, xl, len)) return;
        else if (tryDoMid(y, yl, len)) return;
        else if (tryDoMid(up[x][0], xl+2, len)) return;
    }

    void solve() {
        getBfs(que);
        for (int j=n; j>0; j--) {
            int o=que[j][0];
            for (int i=ind[o]; i; i=link[i]) {
                nval[o] = min(nval[o], nval[to[i]]+val[i]);
            }
        }
        for (int j=1; j<=n; j++) {
            int o=que[j][0];
            for (int i=ind[o]; i; i=link[i]) {
                nval[to[i]] = min(nval[to[i]], nval[o]+val[i]);
            }
        }
    }

    int main() {
        freopen("stone.in", "r", stdin);
        freopen("stone.out", "w", stdout);

        int q, tn;
        read(n);
        initLog(2*n);
        tn = n;
        for (int i=2; i<=n; i++) {
            int f;
            read(f);
            addSide(f, i);
            addSide(i, f);
        }
        build();
        memset(nval, 127, sizeof nval);
        read(q);
        for (int i=1; i<=q; i++) {
            int size, t1, t2, v=0;
            read(size);
            read(t1);
            t2 = t1;
            for (int i=2; i<=size; i++) {
                int t;
                read(t);
                int u1=dis(t1, t), u2=dis(t2, t);
                if (u1>=v && u1>=u2) {
                    t2=t;
                    v = u1;
                } else if (u2>=v && u2>=u1) {
                    t1=t;
                    v = u2;
                }
            }
            getMid(t1, t2, v);
        }

        solve();
        for (int i=1; i<=tn; i++) printf("%d\n", nval[i]/2);

        fclose(stdin);
        fclose(stdout);
        return 0;
    }
};

int main() {return my::main();}
